import 'package:boss/color_constant.dart';
import 'package:boss/models/book_info_list_data.dart';
import 'package:boss/ui/page/commodity/categories/page/plusCommodityMode/page/plusPackage/widget/package_panel.dart';
import 'package:boss/ui/widget/button/fixed_bottom_btn.dart';
import 'package:boss/ui/widget/calendar_bar.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';

final List<Map<String, dynamic>> _status = [
  {'title': '全部', 'value': null},
  {'title': '已用餐', 'value': BookInfo.STATUS_END},
  {'title': '待用餐', 'value': BookInfo.STATUS_BEGIN},
  {'title': '已取消', 'value': BookInfo.STATUS_CANCEL},
];

final List<Map<String, dynamic>> _sex = [
  {'title': '全部', 'value': null},
  {'title': '先生', 'value': BookInfo.SEX_MAN},
  {'title': '女士', 'value': BookInfo.SEX_WOMAN},
];

class BookingSearchBrowser extends StatefulWidget {
  final String status;

  final String titleEnum;

  final DateTime startTime;

  final DateTime endTime;

  final DateTime beginArrive;

  final DateTime endArrive;

  const BookingSearchBrowser({
    this.status,
    this.titleEnum,
    this.startTime,
    this.endTime,
    this.beginArrive,
    this.endArrive,
    Key key,
  }) : super(key: key);

  @override
  _BookingSearchBrowserState createState() => _BookingSearchBrowserState();
}

class _BookingSearchBrowserState extends State<BookingSearchBrowser> {
  String status;

  String titleEnum;

  DateTime startTime;

  DateTime endTime;

  DateTime beginArrive;

  DateTime endArrive;

  @override
  void initState() {
    super.initState();
    status = widget.status;
    titleEnum = widget.titleEnum;
    startTime = widget.startTime;
    endTime = widget.endTime;
    beginArrive = widget.beginArrive;
    endArrive = widget.endArrive;
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Expanded(
          child: SingleChildScrollView(
              padding: EdgeInsets.all(0),
              child: Column(
                children: <Widget>[
                  PackagePanel(
                    title: '预订时间：',
                    children: <Widget>[
                      Column(
                        children: <Widget>[
                          CalendarBar(
                            startDateTime: startTime,
                            endDateTime: endTime,
                            onDateTimeChanged: setStartTime,
                            isShowIcon: false,
                          ),
                          Container(
                            margin: EdgeInsets.only(top: 30.w),
                            child: CalendarBar(
                              startDateTime: beginArrive,
                              endDateTime: endArrive,
                              onDateTimeChanged: setBeginArrive,
                              type: CalendarType.hm,
                              isShowIcon: false,
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                  PackagePanel(
                    title: '预订状态：',
                    children: <Widget>[
                      Wrap(
                        spacing: 20.w,
                        runSpacing: 20.w,
                        children: _status
                            .map((item) => _Chip(
                                  title: item['title'],
                                  isSelected: item['value'] == status,
                                  onTap: () => setStatus(item['value']),
                                ))
                            .toList(),
                      )
                    ],
                  ),
                  PackagePanel(
                    title: '预订人性别：',
                    children: <Widget>[
                      Wrap(
                        spacing: 20.w,
                        runSpacing: 20.w,
                        children: _sex
                            .map((item) => _Chip(
                                  title: item['title'],
                                  isSelected: item['value'] == titleEnum,
                                  onTap: () => setSex(item['value']),
                                ))
                            .toList(),
                      )
                    ],
                  ),
                ],
              )),
        ),
        FixedBottomBtn(
          title: '查 询',
          onTap: () {
            Navigator.pop(context, {
              'status': status,
              'titleEnum': titleEnum,
              'startTime': startTime,
              'endTime': endTime,
              'beginArrive': beginArrive,
              'endArrive': endArrive,
            });
          },
        ),
      ],
    );
  }

  setStatus(String status) {
    this.status = status;
    setState(() {});
  }

  setSex(String sex) {
    this.titleEnum = sex;
    setState(() {});
  }

  setStartTime(List<DateTime> dateTime) {
    startTime = dateTime[0];
    endTime = dateTime[1];
    setState(() {});
  }

  setBeginArrive(List<DateTime> dateTime) {
    beginArrive = dateTime[0];
    endArrive = dateTime[1];
    setState(() {});
  }
}

class _Chip extends StatelessWidget {
  final String title;
  final bool isSelected;
  final VoidCallback onTap;

  const _Chip({this.title, this.isSelected, this.onTap, Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        width: 150.w,
        height: 60.w,
        decoration: BoxDecoration(
          color: isSelected ? ColorConstant.ff5770f6 : Colors.white,
          border: isSelected ? null : Border.all(color: ColorConstant.e5e5),
          borderRadius: BorderRadius.circular(60.w),
        ),
        alignment: Alignment.center,
        child: Text(
          title,
          style: TextStyle(
            color: isSelected ? Colors.white : ColorConstant.ff33,
            fontSize: 28.sp,
          ),
        ),
      ),
    );
  }
}
